package com.family.filter;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 使用注解标注过滤器
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
 * 属性filterName声明过滤器的名称,可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 *
 */
@WebFilter(filterName="myFilter",urlPatterns="/*")
public class MyFilter implements Filter {

    public static  ArrayList<String> arr=new ArrayList<String>();

    public static  ArrayList<String> noarr=new ArrayList<String>();

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());

        if(noInclude(url))
        {
            chain.doFilter(httpRequest, httpResponse);
            return;
        }

        if (!isInclude(url)){
//        	 System.out.println("pass"+url);
            chain.doFilter(httpRequest, httpResponse);
            return;
        } else {
            HttpSession session = httpRequest.getSession();
            if (session.getAttribute("user") != null){
                // session存在
                chain.doFilter(httpRequest, httpResponse);
                return;
            } else {
                // session不存在 准备跳转失败
                RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/");
                dispatcher.forward(httpRequest, httpResponse);
                return;
            }
        }

    }

    @Override
    public void init(FilterConfig config) throws ServletException {


        arr.add("/login_success");
        arr.add("/show_deskgroup");
        arr.add("/show_addfood");
        arr.add("/show_desk");
        arr.add("/show_footevsort");
        arr.add("/show_area");
        arr.add("/show_add_user");
        arr.add("/show_userlist");
        arr.add("/show_add_role");
        arr.add("/show_rolelist");
        arr.add("/show_foodtype");
        arr.add("/show_foodlist");
        arr.add("/show_foodgroup");
        arr.add("/show_orderlist");
        arr.add("/show_foodCount");
        arr.add("/add_user");

        noarr.add("/show_deskstat");
        noarr.add("/p_getMsgLen");
        noarr.add("/p_getMsgNotify");


    }

    private boolean isInclude(String url) {

        for(String str:arr)
        {

            if(url.indexOf(str)>=0)
            {
//    			System.out.println("url="+url+" surl="+str);
                return true;
            }
        }

        return false;
    }

    private boolean noInclude(String url) {
        return noarr.contains(url);
    }

}
